//
//  MyProfileCell.m
//  DebugDemo
//
//  Created by XFB on 2018/11/21.
//  Copyright © 2018年 ZhouCheng. All rights reserved.
//

#import "MyProfileCell.h"
#import "MyProfileItemModel.h"

@interface MyProfileCell ()

@property (nonatomic, strong) UILabel *themeLabel;
@property (nonatomic, strong) UIImageView *themeImageView;
@property (nonatomic, strong) UILabel *detailNameLabel;
@property (nonatomic, strong) UIImageView *detailImageView;
@property (nonatomic, strong) UISwitch *switchView;
@property (nonatomic, strong) UIImageView *indicatorView;

@end

@implementation MyProfileCell

- (void)setItemModel:(MyProfileItemModel *)itemModel
{
    _itemModel = itemModel;

    [self.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

    // 1.主题图片
    if (itemModel.themeImage) {
        [self setupThemeImageView];
    }
    
    // 2.主题名字
    if (itemModel.themeName) {
        [self setupThemeNameLabel];
    }

    // 3.accessoryView
    if (itemModel.accessoryType) {
        [self setupAccessoryView];
    }
    
    // 4.详情名称
    if (itemModel.detailName) {
        [self setupDetailNameLabel];
    }
    
    // 5.详情图片
    if (itemModel.detailImage) {
        [self setupDetailImageView];
    }
}

/// 添加主题图片
- (void)setupThemeImageView
{
    self.themeImageView = [[UIImageView alloc] initWithImage:self.itemModel.themeImage];
    self.themeImageView.x = 15;
    self.themeImageView.centerY = self.contentView.centerY;
    [self.contentView addSubview:self.themeImageView];
}

- (void)setupThemeNameLabel
{
    self.themeLabel = [[UILabel alloc] init];
    self.themeLabel.text = self.itemModel.themeName;
    self.themeLabel.textColor = UIColorFromRGB(0x333333);
    self.themeLabel.font = [UIFont systemFontOfSize:16];
    self.themeLabel.x = CGRectGetMaxX(self.themeImageView.frame) + 15;
    CGSize themeSize = [self.itemModel.themeName sizeWithContentFont:self.themeLabel.font limitWidth:MAXFLOAT];
    self.themeLabel.size = CGSizeMake(themeSize.width, 49);
    self.themeLabel.centerY = self.contentView.centerY;
    [self.contentView addSubview:self.themeLabel];
}

- (void)setupAccessoryView
{
    switch (self.itemModel.accessoryType) {
        case UserCenterAccessoryTypeNone:
            break;
        case UserCenterAccessoryTypeIndicator:
            [self setupIndicatorView];
            break;
        case UserCenterAccessoryTypeSwitch:
            [self setupSwitchView];
            break;
        default:
            break;
    }
}

- (void)setupIndicatorView
{
    [self.contentView addSubview:self.indicatorView];
}

- (void)setupSwitchView
{
    [self.contentView addSubview:self.switchView];
}

- (void)setupDetailNameLabel
{
    self.detailNameLabel = [[UILabel alloc] init];
    self.detailNameLabel.text = self.itemModel.detailName;
    self.detailNameLabel.textColor = UIColorFromRGB(0x666666);
    self.detailNameLabel.font = [UIFont systemFontOfSize:14];
    CGSize detailSize = [self.itemModel.detailName sizeWithContentFont:self.detailNameLabel.font limitWidth:MAXFLOAT];
    self.detailNameLabel.size = CGSizeMake(detailSize.width, 49);
    self.detailNameLabel.centerY = self.contentView.centerY;
    
    switch (self.itemModel.accessoryType) {
        case UserCenterAccessoryTypeNone:
            self.detailNameLabel.x = ScreenW - self.detailNameLabel.width - 15;
            break;
        case UserCenterAccessoryTypeIndicator:
            self.detailNameLabel.x = self.indicatorView.x - self.detailNameLabel.width - 15;
            break;
        case UserCenterAccessoryTypeSwitch:
            self.detailNameLabel.x = self.switchView.x - self.detailNameLabel.width - 15;
            break;
        default:
            break;
    }
    [self.contentView addSubview:self.detailNameLabel];
}

- (void)setupDetailImageView
{
    self.detailImageView = [[UIImageView alloc] init];
    self.detailImageView.centerY = self.contentView.centerY;
    switch (self.itemModel.accessoryType) {
        case UserCenterAccessoryTypeNone:
            self.detailImageView.x = ScreenW - self.detailImageView.width - 15-2;
            break;
        case UserCenterAccessoryTypeIndicator:
            self.detailImageView.x = self.indicatorView.x - self.detailImageView.width - 15;
            break;
        case UserCenterAccessoryTypeSwitch:
            self.detailImageView.x = self.switchView.x - self.detailImageView.width - 15;
            break;
        default:
            break;
    }
}

- (UIImageView *)indicatorView
{
    if (_indicatorView == nil) {
        _indicatorView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myProfile_right_arrow"]];
        _indicatorView.centerY = self.contentView.centerY;
        _indicatorView.x = ScreenW-_indicatorView.width-15;
    }
    return _indicatorView;
}

- (UISwitch *)switchView
{
    if (_switchView == nil) {
        _switchView = [[UISwitch alloc] init];
        _switchView.centerY = self.contentView.centerY;
        _switchView.x = ScreenW - _switchView.width - 15;
        [_switchView addTarget:self action:@selector(switchTouched:) forControlEvents:UIControlEventValueChanged];
    }
    return _switchView;
}

- (void)switchTouched:(UISwitch *)switchView
{
    __weak typeof(self) weakSelf = self;
    self.itemModel.SwitchValueChanged(weakSelf.switchView.isOn);
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
